package com.egao.common.manage.college;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.aolan.service.AoLanService;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.StudentSignSet;
import com.egao.common.module.student.entity.vo.StudentSignAdminListVO;
import com.egao.common.module.student.service.StudentSignService;
import com.egao.common.module.student.service.StudentSignSetService;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import com.egao.common.module.system.service.MajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学院毕业去向管理
 *
 * @author lym
 */
@Api(tags = "学院毕业去向管理")
@RestController
@RequestMapping("${api}")
public class CollegeSignController extends BaseController {

    @Autowired
    private StudentSignService studentSignService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private AoLanService aoLanService;
    @Autowired
    private StudentSignSetService studentSignSetService;

    private final String title = "学院毕业去向管理";

    @PreAuthorize("hasAuthority('college:sign:list')")
    @OperLog(value = title, desc = "分页查询毕业去向")
    @ApiOperation(value = "分页查询毕业去向")
    @ApiPageParam
    @GetMapping("/college/sign/page")
    public PageResult<StudentSignAdminListVO> page(HttpServletRequest request) {
        User loginUser = getLoginUser();

        if (StrUtil.isBlank(loginUser.getCollegeCode())) {
            throw new BusinessException("没有找到您的学院信息，请联系就业中心");
        }

        PageParam<StudentSignAdminListVO> pageParam = new PageParam<>(request);

        pageParam.put("collegeCode", loginUser.getCollegeCode());
        String degree = null;
        degree = UserConstants.DEGREE_SCOPE_ALL.equals(loginUser.getDegreeScope()) ? null : loginUser.getDegreeScope();
        pageParam.put("degree", degree);
        pageParam.put("user", "college");

        if (UserConstants.STUDENT_SCOPE_PART.equals(loginUser.getStudentScope())) {
            pageParam.put("collegeUserId", loginUser.getUserId());
        }

        if (!StrUtil.isBlank(loginUser.getBjhm())) {
            pageParam.put("bjhm", loginUser.getBjhm());
        }

//        String category = pageParam.get("category") != null ? pageParam.get("category").toString() : StudentSignConstants.ADMIN_LIST_CATEGORY_1;
//        if (UserConstants.USER_TYPE_COLLEGE.equals(loginUser.getUserType()) && StudentSignConstants.ADMIN_LIST_CATEGORY_1.equals(category)) {
//            pageParam.put("category", StudentSignConstants.ADMIN_LIST_CATEGORY_8);
//        }

        return studentSignService.listAdminPage(pageParam);
    }

    @PreAuthorize("hasAuthority('college:sign:list')")
    @ApiOperation(value = "查询学院专业")
    @GetMapping("/college/sign/majors")
    public JsonResult majors() {
        return JsonResult.ok().setData(majorService.listAll(getLoginUser().getCollegeCode(), null));
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @OperLog(value = title, desc = "查询详情")
    @ApiOperation("查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/college/sign/detail")
    public JsonResult detail(Integer id) {
        return studentSignService.detail(id, null, true, getLoginUser().getUserType());
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @OperLog(value = title, desc = "选择录用方式", result = true)
    @ApiOperation(value = "选择录用方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PutMapping("/college/sign/pick-dispatch-hired-way")
    public JsonResult pickDispatchHiredWay(@RequestBody StudentSign studentSign) {
        return studentSignService.pickDispatchHiredWay(studentSign, studentSign.getUserId());
    }

    @PreAuthorize("hasAuthority('college:sign:update')")
    @OperLog(value = title, desc = "提交审核", result = true)
    @ApiOperation(value = "提交审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PutMapping("/college/sign/submit")
    public JsonResult submit(@RequestBody StudentSign studentSign) {
        // 院领导
        if (UserConstants.USER_TYPE_COLLEGE.equals(getLoginUser().getUserType())) {
            if (StudentSignConstants.SIGN_STATE_COLLEGE_TERMINATION_PASS.equals(studentSign.getSignState())) {
                studentSign.setSignState(StudentSignConstants.SIGN_STATE_COLLEGE_LEAD_TERMINATION_PASS);
            }
            if (StudentSignConstants.SIGN_STATE_COLLEGE_TERMINATION_FAIL.equals(studentSign.getSignState())) {
                studentSign.setSignState(StudentSignConstants.SIGN_STATE_COLLEGE_LEAD_TERMINATION_FAIL);
            }
        }
        return studentSignService.submitForCollege(studentSign, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('college:sign:update')")
    @OperLog(value = title, desc = "提交修改", result = true)
    @ApiOperation(value = "提交修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSign", value = "studentSign", required = true, dataType = "studentSign")
    })
    @PutMapping("/college/sign/edit")
    public JsonResult edit(@RequestBody StudentSign studentSign) {
        return studentSignService.editForCollege(studentSign, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('college:sign:export')")
    @OperLog(value = title, desc = "导出", result = true)
    @ApiOperation(value = "导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "StudentExcelVO", value = "StudentExcelVO", required = false, dataType = "StudentExcelVO")
    })
    @GetMapping("/college/sign/export")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        User loginUser = getLoginUser();

        if (StrUtil.isBlank(loginUser.getCollegeCode())) {
            throw new BusinessException("没有找到您的学院信息，请联系就业中心");
        }

        PageParam<StudentSignAdminListVO> pageParam = new PageParam<>(request);

        pageParam.put("collegeCode", loginUser.getCollegeCode());
        String degree = null;
        degree = UserConstants.DEGREE_SCOPE_ALL.equals(loginUser.getDegreeScope()) ? null : loginUser.getDegreeScope();
        pageParam.put("degree", degree);
        pageParam.put("user", "college");

        if (UserConstants.STUDENT_SCOPE_PART.equals(loginUser.getStudentScope())) {
            pageParam.put("collegeUserId", loginUser.getUserId());
        }

        studentSignService.exportStudentSign(pageParam, response);
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @ApiOperation("协议就业打印表")
    @ApiPageParam
    @PostMapping("/college/sign/print/deal")
    public JsonResult dealPrint(@RequestBody StudentSign studentSign) {
        if (studentSign == null || studentSign.getId() == null) {
            throw new BusinessException("参数异常");
        }
        return studentSignService.dealPrint(studentSign.getId(), null);
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @ApiOperation("灵活就业打印表")
    @ApiPageParam
    @PostMapping("/college/sign/print/agile")
    public JsonResult agilePrint(@RequestBody StudentSign studentSign) {
        if (studentSign == null || studentSign.getId() == null) {
            throw new BusinessException("参数异常");
        }
        return studentSignService.agilePrint(studentSign.getId(), null);
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @ApiOperation("自由职业打印表")
    @ApiPageParam
    @PostMapping("/college/sign/print/free")
    public JsonResult freePrint(@RequestBody StudentSign studentSign) {
        if (studentSign == null || studentSign.getId() == null) {
            throw new BusinessException("参数异常");
        }
        return studentSignService.freePrint(studentSign.getId(), null);
    }

    @PreAuthorize("hasAuthority('college:sign:detail')")
    @ApiOperation("自主创业打印表")
    @ApiPageParam
    @PostMapping("/college/sign/print/venture")
    public JsonResult venturePrint(@RequestBody StudentSign studentSign) {
        if (studentSign == null || studentSign.getId() == null) {
            throw new BusinessException("参数异常");
        }
        return studentSignService.venturePrint(studentSign.getId(), null);
    }

    @PreAuthorize("hasAuthority('college:sign:update')")
    @OperLog(value = title, desc = "推送奥兰", result = true)
    @ApiOperation(value = "推送奥兰")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/college/sign/ao-lan")
    public JsonResult aoLan(Integer id) {
        StudentSignSet studentSignSet = studentSignSetService.get();
        if (0 != studentSignSet.getAoLan()) {
            return JsonResult.error("推送奥兰功能已关闭");
        }
        StudentSign studentSign = studentSignService.getById(id);
        return aoLanService.insert(studentSign);
    }

    @PreAuthorize("hasAuthority('college:sign:update')")
    @OperLog(value = title, desc = "重置就业方式", result = true)
    @ApiOperation(value = "重置就业方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "毕业去向主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/college/sign/reset")
    public JsonResult reset(Integer id) {
        return studentSignService.resetSignCategoryByCollege(id, getLoginUserId());
    }

}
